package com.example.demo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.entity.OrgRegion;
import com.example.demo.mapper.OrgMapper;
import com.example.demo.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wangmu on 2018/1/29.
 */
@Service(version = "1.0.0")
public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public Map<String, Object> getEmpAscriptionByDeptCode(String code) {
        Map<String, Object> result = new HashMap<>();
        if(code == null || code.equals("")) {
            result.put("status","4001");
            result.put("message","理顾工号不能为空");
            return result;
        }
        try {
            Map<String, Object> list = orgMapper.getEmpAscriptionByDeptCode(code);
            Map<String, Object> collect = list.entrySet()
                    .stream()
                    .filter(e -> e.getValue() != null)
                    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
            result.putAll(collect);
            if(list.isEmpty()) {
                result.put("status","1000");
            } else {
                result.put("status","0000");
                result.put("message","成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status","4000");
            return result;
        }
        return result;
    }

    @Override
    public Map<String, Object> getSubDeptByDeptCode(String deptCode, Integer type) {
        Map<String, Object> result = new HashMap<>();
        if(type == null || type.equals("")) {
            result.put("status","4001");
            result.put("message","部门类型不能为空");
            return result;
        }
        List<OrgRegion> list = null;
        try {
            list = orgMapper.getSubDeptByDeptCode(deptCode, type);
            if(list.isEmpty()) {
                result.put("status","1000");
            } else {
                result.put("status","0000");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status","4000");
            return result;
        }
        result.put("data",list);
        return result;
    }

    @Override
    public Map<String, Object> getEmpCountInDept(String deptCode) {
        Map<String, Object> result = new HashMap<>();
        int count = 0;
        try {
            count = orgMapper.getEmpCountInDept(deptCode);
            if(count>0) {
                result.put("status","0000");
            } else {
                result.put("status","1000");
            }
            result.put("count",count);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status","4000");
            return result;
        }
        return result;
    }
}
